package com.wing.bill.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.dao.DayAndMonthReportForAppDao;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DayAndMonthReportForAppServiceImpl  implements DayAndMonthReportForAppService {

    @Autowired
    private DayAndMonthReportForAppDao dayAndMonthReportForAppDao;
    @Autowired
    private UserClient userClient;

    private static final String TYPE_DAY = "day";
    private static final String TYPE_MONTH = "month";

    //当前时间往前推一个月
    public static List<LocalDateTime> dateUtil() {//当天开始时间00 00 00 到 23 59 59
        LocalDateTime first = LocalDateTime.of(LocalDate.now().plusMonths(-1), LocalTime.MIN);
        LocalDateTime last = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        List list = new ArrayList<>();
        list.add(first);
        list.add(last);
        return list;
    }
    //昨天
    public static List<LocalDateTime> dayUtil() {//当天开始时间00 00 00 到 23 59 59
        LocalDateTime first = LocalDateTime.of(LocalDate.now().plusDays(-1), LocalTime.MIN);
        LocalDateTime last = LocalDateTime.of(LocalDate.now().plusDays(-1), LocalTime.MAX);
        List list = new ArrayList<>();
        list.add(first);
        list.add(last);
        return list;
    }
    //当前时间往前推三个月
    public static List<LocalDateTime> monthUtil() {//当天开始时间00 00 00 到 23 59 59
        LocalDateTime first = LocalDateTime.of(LocalDate.now().plusMonths(-3), LocalTime.MIN);
        LocalDateTime last = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        List list = new ArrayList<>();
        list.add(first);
        list.add(last);
        return list;
    }

    @Override
    public HashMap queryTopNew(String type) {
        if (type.equals(TYPE_DAY)){
            return dayAndMonthReportForAppDao.queryTopNew(dayUtil().get(0),dayUtil().get(1));
        }else if (type.equals(TYPE_MONTH)){
            return dayAndMonthReportForAppDao.queryTopNew(dateUtil().get(0),dateUtil().get(1));
        }
        return null;
    }

    @Override
    public HashMap queryTopOrder(String type) {
        if (type.equals(TYPE_DAY)){
            return dayAndMonthReportForAppDao.queryTopOrder(dayUtil().get(0),dayUtil().get(1));
        }else if (type.equals(TYPE_MONTH)){
            return dayAndMonthReportForAppDao.queryTopOrder(dateUtil().get(0),dateUtil().get(1));
        }
        return null;
    }

    @Override
    public HashMap queryTopIn(String type) {
        if (type.equals(TYPE_DAY)){
            return dayAndMonthReportForAppDao.queryTopIn(dayUtil().get(0),dayUtil().get(1));
        }else if (type.equals(TYPE_MONTH)){
            return dayAndMonthReportForAppDao.queryTopIn(dateUtil().get(0),dateUtil().get(1));
        }
        return null;
    }

    @Override
    public HashMap queryTopCell(String type) {
        if (type.equals(TYPE_DAY)){
            return dayAndMonthReportForAppDao.queryTopCell(dayUtil().get(0),dayUtil().get(1));
        }else if (type.equals(TYPE_MONTH)){
            return dayAndMonthReportForAppDao.queryTopCell(dateUtil().get(0),dateUtil().get(1));
        }
        return null;
    }

    @Override
    public PageResult queryTopUnusualReason(String type,int page,int size) {
        Page pg = new Page<>(page, size);
        if (type.equals(TYPE_DAY)){
            List<HashMap> list =  dayAndMonthReportForAppDao.queryTopUnusualReason(dayUtil().get(0),dayUtil().get(1),pg);
            return new PageResult(list, list.size());
        }else if (type.equals(TYPE_MONTH)){
            List<HashMap> list =   dayAndMonthReportForAppDao.queryTopUnusualReason(dateUtil().get(0),dateUtil().get(1),pg);
            return new PageResult(list, list.size());
        }
        return null;
    }

    @Override
    public PageResult queryEmployeeNew(String type,int page,int size) {
        Page pg = new Page<>(page, size);
        if (type.equals(TYPE_DAY)){
            List<HashMap> list = dayAndMonthReportForAppDao.queryEmployeeNewList(dayUtil().get(0),dayUtil().get(1),pg);
            return new PageResult(list, list.size());
        }else if (type.equals(TYPE_MONTH)){
            List<HashMap> list =dayAndMonthReportForAppDao.queryEmployeeNewList(dateUtil().get(0),dateUtil().get(1),pg);
            return new PageResult(list, list.size());
        }
        return null;
    }

    @Override
    public int queryEmployeeNewNum(String type) {
        if (type.equals(TYPE_DAY)){
            return dayAndMonthReportForAppDao.queryEmployeeNewNum(dayUtil().get(0),dayUtil().get(1));
        }else if (type.equals(TYPE_MONTH)){
            return dayAndMonthReportForAppDao.queryEmployeeNewNum(dateUtil().get(0),dateUtil().get(1));
        }
        return 0;
    }

    @Override
    public PageResult queryOrderMat(String type,int page,int size) {
        Page pg = new Page<>(page, size);
        if (type.equals(TYPE_DAY)){
            List<HashMap> list =dayAndMonthReportForAppDao.queryOrderMat(dayUtil().get(0),dayUtil().get(1),pg);
            return new PageResult(list, list.size());
        }else if (type.equals(TYPE_MONTH)){
            List<HashMap> list =dayAndMonthReportForAppDao.queryOrderMat(dateUtil().get(0),dateUtil().get(1),pg);
            return new PageResult(list, list.size());
        }
        return null;
    }

    @Override
    public PageResult queryDeviceMat(String type,String param,int page,int size) {
        Page pg = new Page<>(page, size);
        if (type.equals(TYPE_DAY)){
            List<HashMap> list = dayAndMonthReportForAppDao.queryDeviceMat(dayUtil().get(0),dayUtil().get(1),param,pg);
            return new PageResult(list, list.size());
        }else if (type.equals(TYPE_MONTH)){
            List<HashMap> list =dayAndMonthReportForAppDao.queryDeviceMat(dateUtil().get(0),dateUtil().get(1),param,pg);
            return new PageResult(list, list.size());
        }
        return null;
    }

    @Override
    public PageResult queryChangeTool(String type,int page,int size) {
        Page pg = new Page<>(page, size);
        if (type.equals(TYPE_DAY)){
            List<HashMap> list = dayAndMonthReportForAppDao.queryChangeTool(dayUtil().get(0),dayUtil().get(1),pg);
            return new PageResult(list, list.size());
        }else if (type.equals(TYPE_MONTH)){
            List<HashMap> list = dayAndMonthReportForAppDao.queryChangeTool(dateUtil().get(0),dateUtil().get(1),pg);
            return new PageResult(list, list.size());
        }
        return null;
    }

    @Override
    public Long queryChangeToolAvg(String type) {
        if (type.equals(TYPE_DAY)){
            return dayAndMonthReportForAppDao.queryChangeToolAvg(dayUtil().get(0),dayUtil().get(1));
        }else if (type.equals(TYPE_MONTH)){
            return dayAndMonthReportForAppDao.queryChangeToolAvg(dateUtil().get(0),dateUtil().get(1));
        }
        return null;
    }


    @Override
    public PageResult queryReturnRatio(int page,int size) {
        Page pg = new Page<>(page, size);
        List<HashMap> list =  dayAndMonthReportForAppDao.queryReturnRatio(monthUtil().get(0),monthUtil().get(1),pg);
        return  new PageResult(list, list.size());
    }

    @Override
    public PageResult queryNoReturn(int page,int size) {
        Page pg = new Page<>(page, size);
        List<HashMap> list =  dayAndMonthReportForAppDao.queryNoReturn(monthUtil().get(0),monthUtil().get(1),pg);
        return  new PageResult(list, list.size());
    }

    @Override
    public int queryNoReturnTotal() {
        return dayAndMonthReportForAppDao.queryNoReturnTotal(monthUtil().get(0),monthUtil().get(1));
    }

    @Override
    public PageResult queryEmployeeLoss(int page,int size) {
        Page pg = new Page<>(page, size);
        List<HashMap> list =  dayAndMonthReportForAppDao.queryEmployeeLoss(monthUtil().get(0),monthUtil().get(1),pg);
        return  new PageResult(list, list.size());
    }

    @Override
    public PageResult queryToolCost(int page,int size) {
        Page pg = new Page<>(page, size);
        List<HashMap> list =  dayAndMonthReportForAppDao.queryToolCost(monthUtil().get(0),monthUtil().get(1),pg);
        return  new PageResult(list, list.size());
    }

    @Override
    public Long queryToolCostAvg() {
        return dayAndMonthReportForAppDao.queryToolCostAvg(monthUtil().get(0),monthUtil().get(1));
    }

    @Override
    public PageResult queryLifeSpan(int page,int size) {
        Page pg = new Page<>(page, size);
        List<HashMap> list =  dayAndMonthReportForAppDao.queryLifeSpan(pg);
        return  new PageResult(list, list.size());
    }

    @Override
    public Long queryLifeSpanAvg() {
        return dayAndMonthReportForAppDao.queryLifeSpanAvg();
    }

    @Override
    public int queryNumOfEmp() {
        JsonResult<List<UserVO>> jsonResult = userClient.getAll();
        return jsonResult.getData().size();
    }
}
